/*
 * JSelect.java
 *
 * Created on 20 agosto 2008, 22.05
 */
package monopoli.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import monopoli.Monopoli;
import monopoli.MonopoliView;
import monopoli.game.Giocatore;
import org.jdesktop.application.Action;

/**
 *
 * @author  Claudio
 */
public class JSelect extends javax.swing.JDialog implements ActionListener {

    private final static long serialVersionUID = 1232131233124237L;
    private MonopoliView frameView;

    public static String FUNGHETTO_STRING = "funghetto";
    public static String CANDELA_STRING = "candela";
    public static String PIANTINA_STRING = "piantina";
    public static String PERA_STRING = "pera";
    public static String MELA_STRING = "mela";
    public static String FIASCO_STRING = "fiasco";

    //JLabel    picture;
    /** Creates new form JSelect */
    public JSelect(java.awt.Frame parent, boolean modal, MonopoliView fv) {
        super(parent, modal);
        frameView = fv;
        initComponents();
        inizializza();
        disabilitaIcone();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblMonopoliJ = new javax.swing.JLabel();
        btnCandela = new javax.swing.JRadioButton();
        btnFunghetto = new javax.swing.JRadioButton();
        btnPiantina = new javax.swing.JRadioButton();
        btnPera = new javax.swing.JRadioButton();
        btnMela = new javax.swing.JRadioButton();
        fiascoButton = new javax.swing.JRadioButton();
        lblSceltaPedina = new javax.swing.JLabel();
        cmdScelta = new javax.swing.JButton();
        lblInizioPartita = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(monopoli.MonopoliApp.class).getContext().getResourceMap(JSelect.class);
        lblMonopoliJ.setFont(resourceMap.getFont("lblMonopoliJ.font")); // NOI18N
        lblMonopoliJ.setText(resourceMap.getString("lblMonopoliJ.text")); // NOI18N
        lblMonopoliJ.setName("lblMonopoliJ"); // NOI18N

        btnCandela.setText(resourceMap.getString("btnCandela.text")); // NOI18N
        btnCandela.setIcon(resourceMap.getIcon("btnCandela.icon")); // NOI18N
        btnCandela.setName("btnCandela"); // NOI18N

        btnFunghetto.setText(resourceMap.getString("btnFunghetto.text")); // NOI18N
        btnFunghetto.setIcon(resourceMap.getIcon("btnFunghetto.icon")); // NOI18N
        btnFunghetto.setName("btnFunghetto"); // NOI18N

        btnPiantina.setText(resourceMap.getString("btnPiantina.text")); // NOI18N
        btnPiantina.setIcon(resourceMap.getIcon("btnPiantina.icon")); // NOI18N
        btnPiantina.setName("btnPiantina"); // NOI18N

        btnPera.setText(resourceMap.getString("btnPera.text")); // NOI18N
        btnPera.setIcon(resourceMap.getIcon("btnPera.icon")); // NOI18N
        btnPera.setName("btnPera"); // NOI18N

        btnMela.setText(resourceMap.getString("btnMela.text")); // NOI18N
        btnMela.setIcon(resourceMap.getIcon("btnMela.icon")); // NOI18N
        btnMela.setName("btnMela"); // NOI18N

        fiascoButton.setText(resourceMap.getString("fiascoButton.text")); // NOI18N
        fiascoButton.setIcon(resourceMap.getIcon("fiascoButton.icon")); // NOI18N
        fiascoButton.setName("fiascoButton"); // NOI18N

        lblSceltaPedina.setFont(resourceMap.getFont("lblSceltaPedina.font")); // NOI18N
        lblSceltaPedina.setText(resourceMap.getString("lblSceltaPedina.text")); // NOI18N
        lblSceltaPedina.setName("lblSceltaPedina"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(monopoli.MonopoliApp.class).getContext().getActionMap(JSelect.class, this);
        cmdScelta.setAction(actionMap.get("Selected")); // NOI18N
        cmdScelta.setFont(resourceMap.getFont("cmdScelta.font")); // NOI18N
        cmdScelta.setText(resourceMap.getString("cmdScelta.text")); // NOI18N
        cmdScelta.setName("cmdScelta"); // NOI18N

        lblInizioPartita.setFont(resourceMap.getFont("lblInizioPartita.font")); // NOI18N
        lblInizioPartita.setText(resourceMap.getString("lblInizioPartita.text")); // NOI18N
        lblInizioPartita.setFocusCycleRoot(true);
        lblInizioPartita.setName("lblInizioPartita"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblSceltaPedina)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnFunghetto)
                            .addComponent(btnCandela)
                            .addComponent(btnPiantina))
                        .addGap(172, 172, 172)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnPera)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(btnMela)
                                .addComponent(fiascoButton))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(lblInizioPartita)))
                .addContainerGap(85, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(188, Short.MAX_VALUE)
                .addComponent(lblMonopoliJ)
                .addGap(176, 176, 176))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(222, Short.MAX_VALUE)
                .addComponent(cmdScelta, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(217, 217, 217))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(lblMonopoliJ, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(lblSceltaPedina)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCandela)
                            .addComponent(btnPera)))
                    .addComponent(lblInizioPartita))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFunghetto)
                    .addComponent(btnMela))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPiantina)
                    .addComponent(fiascoButton))
                .addGap(18, 18, 18)
                .addComponent(cmdScelta, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                JSelect dialog = new JSelect(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void inizializza() {


        //Create the radio buttons.
        //JRadioButton funghettoButton = new JRadioButton(FUNGHETTO_STRING);
        btnFunghetto.setText(FUNGHETTO_STRING);
        btnFunghetto.setMnemonic(KeyEvent.VK_F);
        btnFunghetto.setActionCommand(FUNGHETTO_STRING);
        btnFunghetto.setSelected(true);
        btnFunghetto.setVisible(true);

        //JRadioButton candelaButton = new JRadioButton(CANDELA_STRING);
        btnCandela.setText(CANDELA_STRING);
        btnCandela.setMnemonic(KeyEvent.VK_C);
        btnCandela.setActionCommand(CANDELA_STRING);
        btnCandela.setVisible(true);

        //JRadioButton piantinaButton = new JRadioButton(PIANTINA_STRING);
        btnPiantina.setText(PIANTINA_STRING);
        btnPiantina.setMnemonic(KeyEvent.VK_P);
        btnPiantina.setActionCommand(PIANTINA_STRING);
        btnPiantina.setVisible(true);

        //JRadioButton peraButton = new JRadioButton(PERA_STRING);
        btnPera.setText(PERA_STRING);
        btnPera.setMnemonic(KeyEvent.VK_R);
        btnPera.setActionCommand(PERA_STRING);
        btnPera.setVisible(true);

        //JRadioButton melaButton = new JRadioButton(MELA_STRING);
        btnMela.setText(MELA_STRING);
        btnMela.setMnemonic(KeyEvent.VK_M);
        btnMela.setActionCommand(MELA_STRING);
        btnMela.setVisible(true);


        //JRadioButton fiascoButton = new JRadioButton(FIASCO_STRING);
        fiascoButton.setText(FIASCO_STRING);
        fiascoButton.setMnemonic(KeyEvent.VK_F);
        fiascoButton.setActionCommand(FIASCO_STRING);
        fiascoButton.setVisible(true);

        //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(btnFunghetto);
        group.add(btnCandela);
        group.add(btnPiantina);
        group.add(btnPera);
        group.add(btnMela);
        group.add(fiascoButton);


        //Register a listener for the radio buttons.
        btnFunghetto.addActionListener(this);
        btnCandela.addActionListener(this);
        btnPiantina.addActionListener(this);
        btnPera.addActionListener(this);
        btnMela.addActionListener(this);
        fiascoButton.addActionListener(this);


    }

    /** Listens to the radio buttons. */
    public void actionPerformed(ActionEvent e) {
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = JSelect.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    @Action
    public void Selected() {
        
        //Recupero dati dalla finestra
        int iPedina = 0;

        if (btnFunghetto.isSelected()) {
            iPedina = 1;
        }
        if (btnCandela.isSelected()) {
            iPedina = 2;
        }
        if (btnPiantina.isSelected()) {
            iPedina = 3;
        }
        if (btnPera.isSelected()) {
            iPedina = 4;
        }
        if (btnMela.isSelected()) {
            iPedina = 5;
        }
        if (fiascoButton.isSelected()) {
            iPedina = 6;
        }

        //Invocazione del metodo di interfaccia per comunicare la pedina scelta
        monopoli.Monopoli.getMonopoliGUI().ComunicaPedina(iPedina);
        
        
        //Apertura della finestra di dialogo delle proprieta'
        frameView.showGetProperties();

        //Chiusura della finestra di dialogo corrente
        this.setVisible(false);
        this.dispose();

    }


    public void disabilitaIcone() {
        ArrayList<Giocatore> glist = Monopoli.getMonopoliPartita().getGiocatore();
        Iterator it = glist.iterator();
        while(it.hasNext()) {
            Giocatore g = (Giocatore) it.next();
            int id = g.getPedina().getId();
            if (id == 1) {
                btnFunghetto.setEnabled(false);
            }
            if (id == 2) {
                btnCandela.setEnabled(false);
            }
            if (id == 3) {
                btnPiantina.setEnabled(false);
        }
        if (id == 4) {
            btnPera.setEnabled(false);
        }
        if (id == 5) {
            btnMela.setEnabled(false);
        }
        if (id == 6) {
            fiascoButton.setEnabled(false);
        }

        }

    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton btnCandela;
    private javax.swing.JRadioButton btnFunghetto;
    private javax.swing.JRadioButton btnMela;
    private javax.swing.JRadioButton btnPera;
    private javax.swing.JRadioButton btnPiantina;
    private javax.swing.JButton cmdScelta;
    private javax.swing.JRadioButton fiascoButton;
    private javax.swing.JLabel lblInizioPartita;
    private javax.swing.JLabel lblMonopoliJ;
    private javax.swing.JLabel lblSceltaPedina;
    // End of variables declaration//GEN-END:variables
}
